//
//  mmjLoginOrRegisterTextField.m
//  Social
//
//  Created by seirra on 2018/5/15.
//  Copyright © 2018年 mmj. All rights reserved.
//

#import "mmjLoginOrRegisterTextField.h"
#import <objc/runtime.h>//运行时
static NSString*const mmjplaceholderLabeltextColor = @"placeholderLabel.textColor";



@implementation mmjLoginOrRegisterTextField

-(void)awakeFromNib{
    //设置光标颜色
 
  self.tintColor = [UIColor whiteColor];
//设置默认的占位文字颜色
    self.plachholderColor = [UIColor grayColor];
    //[self setValue:[UIColor grayColor] forKeyPath:mmjplaceholderLabeltextColor];
    
//    [self becomeFirstResponder];//成为第一响应者:开始编辑/弹出键盘/获得焦点
//    [self resignFirstResponder];//失去第一响应者：结束编辑/退出键盘/失去焦点
    
       //运行时
    //    unsigned int count;
//    Ivar* ivarlist =  class_copyIvarList([UITextField class], &count);
//    for (int i =0; i<count; i++) {
//        Ivar ivar = ivarlist[i];
//        NSLog(@"%s",ivar_getName(ivar));
//    }
//    free(ivarlist);

//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
////        UILabel* label = self.subviews.lastObject;
////        label.textColor = [UIColor whiteColor];
//        mmjLog(@"%@",self.subviews.lastObject);
//
//    });
    
//    NSMutableDictionary* dic = [NSMutableDictionary dictionary];
//    dic[NSForegroundColorAttributeName] = [UIColor whiteColor];
//    self.attributedPlaceholder = [[NSAttributedString alloc]initWithString:self.placeholder attributes:dic];//带有颜色属性的占位文字
//
    
   
    
    
   // [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(EditingDidBegin) name:UITextFieldTextDidBeginEditingNotification object:self];
   // [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(EditingDidEnd) name:UITextFieldTextDidEndEditingNotification object:self];
    
    
    //mmjLog(@"%@",self.placeholder);
    
    //addTarget方法
    //self.delegate = self;

//    [self addTarget:self action:@selector(EditingDidBegin) forControlEvents:UIControlEventEditingDidBegin];
//    [self addTarget:self action:@selector(EditingDidEnd) forControlEvents:UIControlEventEditingDidEnd];
//    [self addTarget:self action:@selector(Changed) forControlEvents:UIControlEventValueChanged];
    
    
    //object对象发出了名字为name得通知，就在queue队列中执行block
//    id observer=[[NSNotificationCenter defaultCenter]addObserverForName:UITextFieldTextDidBeginEditingNotification object:self queue:[[NSOperationQueue alloc]init] usingBlock:^(NSNotification * _Nonnull note) {
//        [self setValue:[UIColor whiteColor] forKeyPath:mmjplaceholderLabeltextColor];
//        [[NSNotificationCenter defaultCenter]removeObserver:observer];
//    }];
    
}
//调用时刻：成为第一响应者时 [self becomeFirstResponder];//成为第一响应者:开始编辑/弹出键盘/获得焦点
-(BOOL)becomeFirstResponder{
    self.plachholderColor = [UIColor whiteColor];
    //[self setValue:[UIColor whiteColor] forKeyPath:mmjplaceholderLabeltextColor];
    return [super becomeFirstResponder];
}
//调用时刻：失去第一响应者时 [self resignFirstResponder];//失去第一响应者：结束编辑/退出键盘/失去焦点
-(BOOL)resignFirstResponder{
    //[self setValue:[UIColor grayColor] forKeyPath:mmjplaceholderLabeltextColor];
    self.plachholderColor = [UIColor grayColor];
    return [super resignFirstResponder];
}
#pragma mark <UITextFieldDelegate>
//-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
//
//}
//-(BOOL)textFieldShouldEndEditing:(UITextField *)textField{
//    
//}

///**
// 1.开始编辑 2.获得焦点 3.弹出键盘
// **/
-(void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
     [[NSNotificationCenter defaultCenter]removeObserver:self.observer];
}
-(void)EditingDidBegin{
    //mmjlogfunc;
    mmjLog(@"%@",self.placeholder);
     [self setValue:[UIColor whiteColor] forKeyPath:mmjplaceholderLabeltextColor];
}
///*
// 1.结束编辑 2.失去焦点 3.退下键盘
// */
-(void)EditingDidEnd{
    mmjlogfunc;
     [self setValue:[UIColor grayColor] forKeyPath:mmjplaceholderLabeltextColor];
}
//-(void)Changed{
//    mmjlogfunc;
//}

//-(void)textFieldDidBeginEditing:(UITextField *)textField{
//    [self setValue:[UIColor whiteColor] forKeyPath:mmjplaceholderLabeltextColor];
//}
//-(void)textFieldDidEndEditing:(UITextField *)textField{
//    [self setValue:[UIColor grayColor] forKeyPath:mmjplaceholderLabeltextColor];
//}

#pragma mark --重写
//-(CGRect)placeholderRectForBounds:(CGRect)bounds{
//    //文字属性
//    NSMutableDictionary* dic = [NSMutableDictionary dictionary];
//    dic[NSForegroundColorAttributeName] = [UIColor whiteColor];
//    dic[NSFontAttributeName] = self.font;
//    //画出占位文字
//    [self.placeholder drawInRect:CGRectMake(0, 0, 100, 20) withAttributes:dic];
//    return CGRectMake(0, 0, 100, 20);
//}

//-(void)drawPlaceholderInRect:(CGRect)rect{
//    //文字属性
//        NSMutableDictionary* dic = [NSMutableDictionary dictionary];
//        dic[NSForegroundColorAttributeName] = [UIColor whiteColor];
//        dic[NSFontAttributeName] = self.font;
//        //画出占位文字
//    CGRect placeholder;
//    placeholder.size.height = self.font.lineHeight;;
//    placeholder.size.width = rect.size.width;
//    placeholder.origin.x = 0;
//    placeholder.origin.y = (rect.size.height - self.font.lineHeight)*0.5;
//    [self.placeholder drawInRect:placeholder withAttributes:dic];
//}
@end
